//
//  GameDetailController.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/5.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD
import SafariServices

class GameDetailController: BaseViewController {
    
    var gameId : String = "-999"
    
    var dataModel : GameDetailModel?
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.title = "游戏详情"
        
        
        weak var weakSelf = self
        plainTableView.mj_header =  ZMRefreshHeader.init {
            
            weakSelf!.loadData()
            
        }
        
        plainTableView.estimatedRowHeight = 100
        
        plainTableView.separatorStyle = .none
        
        plainTableView.mj_header.beginRefreshing()
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: GameDetailTopCell.self), bundle: nil), forCellReuseIdentifier: GameDetailTopCell.identifer())
        plainTableView.register(UINib(nibName: StringFromClass(classType: GameDetailSetionCell.self), bundle: nil), forCellReuseIdentifier: GameDetailSetionCell.identifer())
        plainTableView.register(UINib(nibName: StringFromClass(classType: GameDetailTextCell.self), bundle: nil), forCellReuseIdentifier: GameDetailTextCell.identifer())
        plainTableView.register(UINib(nibName: StringFromClass(classType: GameDetailImagesCell.self), bundle: nil), forCellReuseIdentifier: GameDetailImagesCell.identifer())
        

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

   
}


extension GameDetailController{


    func loadData() {
        
        var parmeter  = [String : AnyObject]()
        
        
        parmeter["game_id"] = gameId as AnyObject
        
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=games&a=detail", parameters: parmeter) {[weak self] (responseObject, error) in
            
            
            if error == nil {
                
                guard let temp = responseObject?["datas"]?["merchant"] as? [String : AnyObject] else{
                    
                    self!.plainTableView.mj_header.endRefreshing()
                    
                    return
                    
                }
                
                self!.dataModel = GameDetailModel(dic:temp )
                
                
                self!.plainTableView.mj_header.endRefreshing()
                
                
                self!.plainTableView.reloadData()
                
                
            }else{
                
                self!.plainTableView.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
            
            
        }
        
        

        
        
    }

    
    
    
    
    
    
}





// MARK:- tableview代理方法
extension GameDetailController{
    
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataModel == nil {
            return 0
        }
        
        return 1
        
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if dataModel == nil {
            return 0
        }
        
        if dataModel!.imgs.count > 0 {
            
            return 5

        } else {
            
            return 3

        }
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var tableCell : UITableViewCell? = nil
        
        
        switch indexPath.row {
           case 0:
            let tempCell : GameDetailTopCell =  tableView.dequeueReusableCell(withIdentifier: GameDetailTopCell.identifer()) as! GameDetailTopCell
            
            tempCell.model = dataModel!
            
            tempCell.delegate = self
            
            tableCell = tempCell

            case 1:
                let tempCell : GameDetailSetionCell =  tableView.dequeueReusableCell(withIdentifier: GameDetailSetionCell.identifer()) as! GameDetailSetionCell
                
                tempCell.textStr = "游戏介绍"
                
                tableCell = tempCell

            case 2:
                let tempCell : GameDetailTextCell =  tableView.dequeueReusableCell(withIdentifier: GameDetailTextCell.identifer()) as! GameDetailTextCell
                
                tempCell.textStr = dataModel!.game_desc
                
                
                tableCell = tempCell

            case 3:
                let tempCell : GameDetailSetionCell =  tableView.dequeueReusableCell(withIdentifier: GameDetailSetionCell.identifer()) as! GameDetailSetionCell
                
                tempCell.textStr = "游戏详情"

                tableCell = tempCell

            
        default:
            
            let tempCell : GameDetailImagesCell =  tableView.dequeueReusableCell(withIdentifier: GameDetailImagesCell.identifer()) as! GameDetailImagesCell
            
            tempCell.model = dataModel!
            
            tableCell = tempCell

            
        }
        
        
        return tableCell!
    }
    
    
    
    
}



extension GameDetailController : GameDetailTopCellDeleage{


    func buyBtnDidTap() {
        
        
        
            
        let tempVC = UIAlertController(title: nil, message: "输入购买信息", preferredStyle: .alert)
        
        let action1 = UIAlertAction(title: "取消", style: .default) { (action) in
            
        }
        
        
            
        let action2 = UIAlertAction(title: "确定", style: .default) { (action) in
                
                let gameNumberField = tempVC.textFields?.first
                let gameRoomIdField = tempVC.textFields?.last
            
            
            if  gameNumberField?.text == nil{
                
                gameNumberField?.text = ""
            }
            
            if  gameRoomIdField?.text == nil{
                
                gameRoomIdField?.text = ""
            }
            
            if gameNumberField!.text!.characters.count < 1{
                
                let tempVC0 = UIAlertController(title: nil, message: "请输入购买房卡数量!", preferredStyle: .alert)
                
                
                
                let action01 = UIAlertAction(title: "取消", style: .default) { (action) in
                    
                }
                
                
                let action02 = UIAlertAction(title: "确定", style: .default) { (action) in
                    
                }
                
                
                
                tempVC0.addAction(action01)
                tempVC0.addAction(action02)
                
                self.present(tempVC0, animated: true, completion: nil)
                
                
                
                return
            }
            
                let number3 = Int(gameNumberField!.text!) ?? 0
            
                if number3 < 1 {
                    
                    
                    let tempVC1 = UIAlertController(title: nil, message: "请输入有效的购买数量!", preferredStyle: .alert)
                    
                    
                    
                    let action11 = UIAlertAction(title: "取消", style: .default) { (action) in
                        
                    }
                    
                    
                    let action12 = UIAlertAction(title: "确定", style: .default) { (action) in
                        
                    }
                    
                    
                    
                    tempVC1.addAction(action11)
                    tempVC1.addAction(action12)
                    
                    self.present(tempVC1, animated: true, completion: nil)
                    
                    
                    
                    return
                    
                    }
            
            
            
            
            if gameRoomIdField!.text!.characters.count < 1{
                
                let tempVC2 = UIAlertController(title: nil, message: "请输入游戏账户ID!", preferredStyle: .alert)
                
                
                
                let action21 = UIAlertAction(title: "取消", style: .default) { (action) in
                    
                }
                
                
                let action22 = UIAlertAction(title: "确定", style: .default) { (action) in
                    
                }
                
                
                
                tempVC2.addAction(action21)
                tempVC2.addAction(action22)
                
                self.present(tempVC2, animated: true, completion: nil)
                
                
                
                return
            }
            
            
            let pushV = GameOrderVC()
            
            pushV.gameID = gameRoomIdField?.text
            
            pushV.shopInfoStr = self.dataModel!.game_id + "|" + "\(number3)"
            
            QL1(pushV.gameID)
            QL1(pushV.shopInfoStr)
            
            self.navigationController?.pushViewController(pushV, animated: true)


            
}


        

        
        
        tempVC.addTextField { (textField) in
            textField.keyboardType = .numberPad
            textField.font = UIFont.systemFont(ofSize: 15)

            
            // 根据原有字符串生成属性字符串
            let placeholderStr = "请输入购买张数"
            
            let strM = NSMutableAttributedString(string:placeholderStr )
            
            strM.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 15), range:NSMakeRange(0, placeholderStr.characters.count))
            
            strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.colorWith(r: 199, g: 199, b: 205), range:NSMakeRange(0, placeholderStr.characters.count) )
            
            textField.attributedPlaceholder = strM
            
            
            
        }
        
        
        tempVC.addTextField { (textField) in
            textField.keyboardType = .numbersAndPunctuation
            textField.font = UIFont.systemFont(ofSize: 15)
            // 根据原有字符串生成属性字符串
            let placeholderStr = "请输入游戏账户ID"
            
            let strM = NSMutableAttributedString(string:placeholderStr )
            
            strM.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 15), range:NSMakeRange(0, placeholderStr.characters.count))
            
            strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.colorWith(r: 199, g: 199, b: 205), range:NSMakeRange(0, placeholderStr.characters.count) )
            
            textField.attributedPlaceholder = strM
            
            
            
        }

        
            tempVC.addAction(action1)
            tempVC.addAction(action2)
            
        present(tempVC, animated: true, completion: nil)
        
            
        
        

        
        
        
        
        
    }
    
    
    


    func downLoadBtnDidTap() {
        
        let urlStr = URL(string: dataModel!.game_ios_download_url)
        
        if urlStr == nil {
            return
        }
        
        
        if #available(iOS 9.0, *) {
            
            let temp = SFSafariViewController(url: urlStr!)
            present(temp, animated: true, completion: nil)
            
        } else {
            
            if UIApplication.shared.canOpenURL(urlStr!) {
                
                UIApplication.shared.openURL(urlStr!)
                
                
            }
            
            
        }
        
        
        
    }

}

